package utils;

import java.io.File;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import actions.runnable.JobMonitor;




import data.WebSocket;

public class Utils {
	public static boolean DEBUG = true;
	public static boolean MOCK = true;
	public static WebSocket ws;
	public static String username;
	public static String passwd;
	public static Map<String,JobMonitor> jobs = Collections.synchronizedMap(new HashMap<String,JobMonitor>());
	public static ExecutorService exec = Executors.newCachedThreadPool();
	
	public static void out(Object o) {
		if(DEBUG) {
			System.out.println(o.toString());
		}
	}

	public static int ConvertWalltime(String w)
	{
		String [] a = w.split(":");
		return Integer.parseInt(a[2])+ Integer.parseInt(a[1])*60 + Integer.parseInt(a[0])*3600;
	}
	
	public static String getMd5Digest(String pInput) {
		try {
			MessageDigest lDigest = MessageDigest.getInstance("MD5");
			lDigest.update(pInput.getBytes());
			BigInteger lHashInt = new BigInteger(1, lDigest.digest());
			return String.format("%1$032X", lHashInt);
		} catch (NoSuchAlgorithmException lException) {
			throw new RuntimeException(lException);
		}
	}
	
	public static boolean removeDirectory(File directory) {
		  if (directory == null)
		    return false;
		  if (!directory.exists())
		    return true;
		  if (!directory.isDirectory())
		    return false;
		  String[] list = directory.list();
		  if (list != null) {
		    for (int i = 0; i < list.length; i++) {
		      File entry = new File(directory, list[i]);
		      if (entry.isDirectory())
		      {
		        if (!removeDirectory(entry))
		          return false;
		      }
		      else
		      {
		        if (!entry.delete())
		          return false;
		      }
		    }
		  }

		  return directory.delete();
	}
}
